/************************************************************
 *  * Hyphenate CONFIDENTIAL
 * __________________
 * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
 *
 * NOTICE: All information contained herein is, and remains
 * the property of Hyphenate Inc.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Hyphenate Inc.
 */

#import "AppDelegate+EaseMob.h"

#import "BDTabBarViewController.h"
#import "BDUserModel.h"
#import "BDGuideViewController.h"

/**
 *  本类中做了EaseMob初始化和推送等操作
 */

@implementation AppDelegate (EaseMob)

- (void)easemobApplication:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
                    appkey:(NSString *)appkey
              apnsCertName:(NSString *)apnsCertName
               otherConfig:(NSDictionary *)otherConfig
{
    //注册登录状态监听
//    [[NSNotificationCenter defaultCenter] addObserver:self
//                                             selector:@selector(loginStateChange:)
//                                                 name:KNOTIFICATION_LOGINCHANGE
//                                               object:nil];
    
//    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
//    BOOL isHttpsOnly = [ud boolForKey:@"identifier_httpsonly"];
    
//    [[EaseSDKHelper shareHelper] hyphenateApplication:application
//                    didFinishLaunchingWithOptions:launchOptions
//                                           appkey:appkey
//                                     apnsCertName:apnsCertName
//                                      otherConfig:nil];
    
    //获取当前版本判断是否需要显示引导页
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    // app版本
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    
//    [ChatDemoHelper shareHelper];
    
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:app_Version] boolValue] == NO) {
        BDGuideViewController *guideVC = [[BDGuideViewController alloc] init];
        self.window.rootViewController = guideVC;
        guideVC.finishedGuideBlock = ^{
            BDTabBarViewController *tabbarVC = [[BDTabBarViewController alloc] init];
            self.mainController = tabbarVC;
            self.window.rootViewController = tabbarVC;
        };
    }else{
        BDTabBarViewController *tabbarVC = [[BDTabBarViewController alloc] init];
        self.mainController = tabbarVC;
        self.window.rootViewController = tabbarVC;
    }
}

- (void)changeRootVC{
    
}

- (void)easemobApplication:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
{
//    [[EaseSDKHelper shareHelper] hyphenateApplication:application didReceiveRemoteNotification:userInfo];
}

#pragma mark - App Delegate

// 将得到的deviceToken传给SDK
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
//    [[NSUserDefaults standardUserDefaults] setObject:deviceToken forKey:@"currentDeviceToken"];
//    BDUserModel *user = [BDUserModel getCurrentUser];
//    if (user.is_guest == NO) {
//        [user bindDeviceToken];
//    }
//    [[EMClient sharedClient] registerForRemoteNotificationsWithDeviceToken:deviceToken completion:^(EMError *aError) {
//        if (aError == nil) {
//            // BDLog(ASLocalizedString(@"绑定token成功"));
//        }else{
//            // BDLog(ASLocalizedString(@"绑定token失败"));
//        }
//    }];
}

// 注册deviceToken失败，此处失败，与环信SDK无关，一般是您的环境配置或者证书配置有误
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"apns.failToRegisterApns", Fail to register apns)
                                                    message:error.description
                                                   delegate:nil
                                          cancelButtonTitle:NSLocalizedString(@"ok", @"OK")
                                          otherButtonTitles:nil];
    [alert show];
}

#pragma mark - EMPushManagerDelegateDevice

// 打印收到的apns信息
-(void)didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    NSError *parseError = nil;
    NSData  *jsonData = [NSJSONSerialization dataWithJSONObject:userInfo
                                                        options:NSJSONWritingPrettyPrinted error:&parseError];
    NSString *str =  [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"apns.content", @"Apns content")
                                                    message:str
                                                   delegate:nil
                                          cancelButtonTitle:NSLocalizedString(@"ok", @"OK")
                                          otherButtonTitles:nil];
    [alert show];
    
}

@end
